Vectors, Arrays and Matrices

http://quant-econ.net/jl/julia_arrays.html

以下の差分方程式を考える.

$$ \begin{eqnarray} X_{t+1} = AX_t + b + \Sigma W_{t + 1}\\ \\ \end{eqnarray} $$$$ \begin{eqnarray} where \hspace{5pt} && X_t : n \times 1, \ A: n \times n, \ b: n \times 1, \ \Sigma: n \times n, \ W_t: n \times 1 \\[10pt] && W_t \hspace{5pt} \ i.i.d. \sim F, \ E[W_t] = \left( \begin{array}{c} 0 \\ 0 \\ \vdots \\ 0 \end{array} \right), \ Var(W_t) = \left( \begin{array}{c} 1, 0, 0, \ldots, 0 \\ 0, 1, 0, \ldots, 0 \\ \ddots \\ 0, 0, 0, \ldots, 1 \end{array} \right) \\ \end{eqnarray} $$

モジュールのインポート


In [1]:
using QuantEcon
using Distributions

In [2]:
EPSILON = 1e-12

function compute_asymptotic_var(A, Sigma, loop_max=1e^5)
    V = Sigma * Sigma'
    S = V
    i = 0
    while i < loop_max
        next_S = A * S * A' + V
        if norm(S - next_S) < EPSILON
            break
        end
        S = next_S
        i += 1
    end
    return S
end


Out[2]:
compute_asymptotic_var (generic function with 2 methods)

In [9]:
A = [0.8 -0.2; -0.1 0.7]
Sigma = [0.5 0.4; 0.4 0.6]

less_than_1 =  all(abs(eigvals(A)) .< 1)
println("固有値: ", eigs)
println("最大固有値は1未満か: ", less_than_1)


固有値: Complex{Float64}[0.0 + 1.0im,0.0 - 1.0im]
最大固有値は1未満か: true

In [12]:
s = compute_asymptotic_var(A, Sigma)
println("漸近分散: ", s)


漸近分散: [0.6712314073191568 0.6334739702534651
 0.6334739702534651 0.8588744279167456]

In [13]:
println(solve_discrete_lyapunov(A, Sigma * Sigma'))


[0.671231407322655 0.6334739702517161
 0.633473970251716 0.8588744279176201]

両者は一致.